package com.xizhi.zomprogress.dao;

import com.xizhi.zomprogress.bean.User;
import com.xizhi.zomprogress.dao.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by admin on 2018/4/11.zom_invited_user
 */
@Repository
public class UserDao extends BaseDao {
    private  static final Logger logger= LoggerFactory.getLogger(UserDao.class);

    @Autowired
    private UserMapper userMapper;

    public void addUser(String userName,String userImage,String invitedUser,String signedUser,String alreadyObtain) {
        jdbcTemplate.update("insert into zom_invited_user(user_name,user_image,invited_user,signed_user,already_obtain) values(?, ?,?, ?,?)", userName,userImage,invitedUser,signedUser,alreadyObtain);
    }

    public List<User> selectAllUsers() {
        String sql="SELECT * FROM `zom_invited_user`";
        logger.debug(sql);
        List<User> users=jdbcTemplate.query(sql,userMapper);
        return users;
    }
//
//    public User selectUserById(int id) {
//        String sql="SELECT * FROM `user` WHERE `id` = ? ";
//        logger.debug(sql+"[parameter:"+id+"]");
//        List<User> users=jdbcTemplate.query(sql,userMapper,id);
//        return users==null?null:users.get(0);
//    }
//    public User selectUserByIdName(int id,String name) {
//        String sql="SELECT * FROM `user` WHERE `id` = ? and `name` = ? ";
//        logger.debug(sql+"[parameter:"+id+"name"+name+"]");
//        List<User> users=jdbcTemplate.query(sql,userMapper,id,name);
//        return users==null?null:users.get(0);
//    }

    public List<User> getUserByInvitedUser(String invitedUser) {
        String sql="SELECT * FROM `zom_invited_user` WHERE `invited_user` = ?  ";
        logger.debug(sql);
        List<User> users=jdbcTemplate.query(sql,userMapper,invitedUser);
        return users;
    }

    public List<User> cheakRepeatUser(String userName,String invitedUser) {
        String sql="SELECT * FROM `zom_invited_user` WHERE `user_name` = ? and `invited_user` = ? ";
        logger.debug(sql);
        List<User> users=jdbcTemplate.query(sql,userMapper,userName,invitedUser);
        return users;
    }
    public List<User> getSiginUserList(String invitedUser,String signedUser) {
        String sql="SELECT * FROM `zom_invited_user` WHERE `invited_user` = ? and `signed_user` = ? ";
        logger.debug(sql);
        List<User> users=jdbcTemplate.query(sql,userMapper,invitedUser,signedUser);
        return users;
    }

        public int diamondSign(String signedUser, int id) {
//         String sql="SELECT * FROM `zom_invited_user` WHERE  `signed_user` = ? and `id` = ? ";
       String sql="update `zom_invited_user` set `signed_user` = ?  where `id` = ? ";
//       String sql="update  zom_invited_user  set signed_user =replace(signed_user ,'no','yes') where `id` =1 ";
//            String sql="Update zom_invited_user Set signed_user ='yes' Where id ='1001' ";

        logger.debug(sql+"[parameter:"+id+"signedUser"+signedUser+"]");
            int rows=jdbcTemplate.update(sql,signedUser,id);
        return rows;
    }
}
